//
//  AreaHomeViewController.m
//  VietQuan
//
//  Created by hainguyen on 11/13/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "AreaHomeViewController.h"
#import "Province.h"
#import "ProvinceExtension.h"
#import "District.h"
#import "DistrictExtention.h"
#import "DistrictCell.h"
#import "AreaSectionHeader.h"
#import "AreaSectionFooter.h"

@implementation AreaHomeViewController
@synthesize searchResults;
@synthesize table;
@synthesize provinceList;
@synthesize districtCell;
@synthesize theSearch;

- (void)search{
    if ([[theSearch.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
        [searchResults setArray:[District getDistricts]];
    }else {
        [searchResults setArray:[District searchByName:theSearch.text]];
    }
    [table reloadData];
}

#pragma mark -
#pragma mark UISearchBar Delegate

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    isSearching = YES;
    [theSearch setShowsCancelButton:YES animated:YES];
    [self search];
    
    return YES;
}
// return NO to not become first responder

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    isSearching = YES;
    [self search];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar{
    isSearching = NO;
    [theSearch setShowsCancelButton:NO animated:YES];
    [theSearch resignFirstResponder];
    [table reloadData];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    isSearching = NO;
    provinceList  = [[NSMutableArray alloc] initWithArray:[Province getProvinces]];
    searchResults = [[NSMutableArray alloc] init];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void) dealloc
{
    [table release];
    [searchResults release];
    [theSearch release];
    [super dealloc];
}

#pragma -
#pragma Table View Data Source Methods
 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (isSearching) {
        return 1;
    }
    
    return [provinceList count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (isSearching) {
        return 0;
    }
    
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (isSearching) {
        return 0;
    }
    
    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (isSearching) {
        return nil;
    }
    AreaSectionFooter *footer = [[[NSBundle mainBundle] loadNibNamed:@"AreaSectionFooter" owner:self options:nil] lastObject];
    
    return footer;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (isSearching) {
        return nil;
    }
    Province *provine = [provinceList objectAtIndex:section];
    AreaSectionHeader *header = [[[NSBundle mainBundle] loadNibNamed:@"AreaSectionHeader" owner:self options:nil] lastObject];
    [header.provineNameLabel setText:provine.name];
    
    return header;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (isSearching) {
        return [searchResults count];
    }
    Province *provine = [provinceList objectAtIndex:section];
    NSArray *ditricts = [District getDistrictWithProvince:[provine.id intValue]];
    
    return [ditricts count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *newsCellId = @"AreaCellID";
	districtCell = (DistrictCell*)[tableView dequeueReusableCellWithIdentifier:newsCellId];
    if (districtCell == nil) {
        [[[NSBundle mainBundle] loadNibNamed:@"DistrictCell" owner:self options:nil] lastObject];
    }
    District *district = nil;
    Province *provine = nil;
    if (isSearching) {
        district = [searchResults objectAtIndex:indexPath.row];
        provine = [Province getProvince:[district.provinceId intValue]];
    } else{
        provine = [provinceList objectAtIndex:indexPath.section];
        NSArray *districtArray = [District getDistrictWithProvince:[provine.id intValue]];
        district = [districtArray objectAtIndex:indexPath.row];
    }
    if (isSearching) {
        [districtCell.districtNameLabel setText:[NSString stringWithFormat:@"%@ - %@", district.name, provine.name]];
    } else {
        [districtCell.districtNameLabel setText:district.name];
    }
    
    return districtCell;
}

#pragma Table View Delegate

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 38.00;
}

@end
